<?php

namespace app\admin\model;

use think\Model;

use think\Db;
use think\Request;
use think\Exception;//事务处理

class Co_evaluate extends Model
{
    //公司风评的添加编辑
    public function addeditevaluate($data)
    {
        $ckdata = notEmpty(['comment' => '评价']);
        if ($ckdata['status'] == 2) {
            return $ckdata;
        }
        if (isset($data['co_id']) && $data['co_id']) {
            $infos = Db::name('company')->where('id', $data['co_id'])->value('status');
            if ($infos == 'LOCK') {
                return array('msg' => $data['name'] . lang("status_locked_can't_be_edit"), 'status' => 0);
            }
        }
        $admin = session('admin');
        $update_date = getTimeDate(null, 12);
        if ($data['biaozhi'] == 'evaluateadd') {
            if (isset($data['isedit']) && $data['isedit'] == 1) {
                $data['create_id'] = $data['update_id'] = session('userid');
                $data['create_at'] = $data['update_at'] = time();
                $co_evaluate = $this->strict(false)->insert($data);
                if ($co_evaluate) {
                    return array('msg' =>lang("added_successfully"), 'status' => 1);
                } else {
                    return array('msg' =>lang("add_failed"), 'status' => 0);
                }
            }else{
                return array(
                    'msg' => lang("added_successfully"),
                    'status' => 1,
                    'update_at' => time(),
                    'update_date' => $update_date,
                    'update_id' => $admin['id'],
                    'updatename' => $admin['nickname'],
                );
            }
        } elseif ($data['biaozhi'] == 'evaluateedit') {
            if (isset($data['isedit']) && $data['isedit'] == 1) {
                unset($data['isedit']);
                unset($data['biaozhi']);
                unset($data['evaluateaddedit']);
                $id = $data['id'];
                unset($data['id']);
                $data['update_id'] = session('userid');
                $data['update_at'] = time();
                $upid = $this->where('id', $id)->update($data);
                if ($upid !== false) {
                    return array('msg' => lang("edited_successfully"), 'status' => 1);
                } else {
                    return array('msg' =>lang("edit_failed"), 'status' => 0);
                }
            }else{
                return array(
                    'msg' => lang("edited_successfully"),
                    'status' => 1,
                    'update_at' => time(),
                    'update_date' => $update_date,
                    'update_id' => $admin['id'],
                    'updatename' => $admin['nickname'],
                );
            }
        }
    }

    //公司风评的删除
    public function delevaluate($data)
    {
        $branchid = $this->where('id', $data['evaluateid'])->delete();
        if ($branchid) {
            return array('msg' => lang("deleted_successfully"), 'status' => 1);
        } else {
            return array('msg' => lang("failed_to_delete"), 'status' => 0);
        }
    }
}